package com.flx.noodle.websocket.controller;

import com.flx.noodle.websocket.entity.ChatMessage;
import com.flx.noodle.websocket.entity.SSOUser;
import com.flx.noodle.websocket.searcher.MessageSearcher;
import com.flx.noodle.websocket.service.ChatService;
import com.flx.noodle.websocket.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import util.JsonOut;
import util.WsConstant;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by fenglixiong on 2017/10/4.
 */
@Controller
@RequestMapping("/message")
public class MessageController {

    @Autowired
    private MessageService messageService;

    @Autowired
    private ChatService chatService;

    @RequestMapping("/messageList.htm")
    public String messageList(@RequestParam(value = "page",required = false,defaultValue = "1")Integer page,
                              @RequestParam(value = "pageSize",required = false,defaultValue = "10")Integer pageSize,
                              @RequestParam(value = "chatId",required = false)Integer chatId,
                              @RequestParam(value = "userId",required = false)Integer userId,
                              @RequestParam(value = "content",required = false)String content,
                              HttpServletRequest request, ModelMap model){
        MessageSearcher searcher = new MessageSearcher();
        searcher.setPage(page);
        searcher.setPageSize(pageSize);
        searcher.setChatId(chatId);
        searcher.setUserId(userId);
        searcher.setContent(content);
        List<ChatMessage> messageList = messageService.findMessageList(searcher);
        model.addAttribute("ps",searcher);
        model.addAttribute("messageList",messageList);
        return "message/message_list";
    }

    @RequestMapping("/messageAdd.htm")
    public String addMessage(){
        return "message/message_add";
    }

    @RequestMapping("/messageEdit.htm")
    public String editMessage(@RequestParam(value = "messageId")Integer messageId,ModelMap model){
        ChatMessage chatMessage = messageService.getMessageById(messageId);
        model.addAttribute("message",chatMessage);
        return "message/message_add";
    }

    @RequestMapping("/saveOrUpdate.htm")
    public String saveOrUpdate(@ModelAttribute ChatMessage chatMessage, HttpServletRequest request){
        SSOUser ssoUser = (SSOUser)request.getSession().getAttribute(WsConstant.WEBSOCKET_SESSION_KEY);
        if(chatMessage.getUserId()==null){
            chatMessage.setUserId(ssoUser.getUserId());
        }
        messageService.saveOrUpdate(chatMessage);
        return "redirect:/message/messageList.htm";
    }

    @RequestMapping("/messageDelete.htm")
    public String deleteMessage(@RequestParam(value = "messageId")Integer messageId){
        messageService.deleteMessage(messageId);
        return "redirect:/message/messageList.htm";
    }

    /**
     * 获取新消息
     * @param page
     * @param pageSize
     * @param chatId
     * @param userId
     * @param content
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("/getNewMsg.json")
    public @ResponseBody
    JsonOut getNewMessage(@RequestParam(value = "page",required = false,defaultValue = "1")Integer page,
                          @RequestParam(value = "pageSize",required = false,defaultValue = "10")Integer pageSize,
                          @RequestParam(value = "chatId",required = false)Integer chatId,
                          @RequestParam(value = "userId",required = false)Integer userId,
                          @RequestParam(value = "content",required = false)String content,
                          HttpServletRequest request, ModelMap model){
        Map<String,Object> retMap = new HashMap<String,Object>();
        MessageSearcher searcher = new MessageSearcher();
        searcher.setPage(page);
        searcher.setPageSize(pageSize);
        searcher.setChatId(chatId);
        searcher.setUserId(userId);
        searcher.setContent(content);
        List<ChatMessage> messageList = messageService.findMessageList(searcher);
        retMap.put("ps",searcher);
        retMap.put("messageList",messageList);
        return new JsonOut(1,"获取成功！",retMap);
    }

    /**
     * 发送新消息
     */
    @RequestMapping("/sendMsg.json")
    public @ResponseBody
    JsonOut sendMsg(@RequestParam(value = "chatId")Integer chatId,
                    @RequestParam(value = "content")String content,
                    HttpServletRequest request){
        SSOUser ssoUser = (SSOUser)request.getSession().getAttribute(WsConstant.WEBSOCKET_SESSION_KEY);
        if(ssoUser==null){
            return new JsonOut(-1,"用户未登录");
        }
        ChatMessage chatMessage = new ChatMessage();
        chatMessage.setUserId(ssoUser.getUserId());
        chatMessage.setUserName(ssoUser.getUserName());
        chatMessage.setChatId(chatId);
        chatMessage.setContent(content);
        chatMessage.setContentType(WsConstant.CHAT_TYPE_TXT);
        chatMessage.setContentSize(content.getBytes().length);
        chatMessage.setCreateTime(new Date());
        int ret = chatService.sendNewMessage(chatMessage);
        if(ret>0) {
            return new JsonOut(1, "发送成功！");
        }else {
            return new JsonOut(-2,"发送失败！");
        }
    }

}
